//
//  RelativeNewsTableView.m
//  item-NetEaseNews-tyw
//
//  Created by tyw on 15/10/3.
//  Copyright (c) 2015年 tyw. All rights reserved.
//

#import "RelativeNewsTableView.h"
#import "RelativeNewsTableViewCell.h"

#import "NewsDetailViewController.h"

#define relativeCellHeight 80

@implementation RelativeNewsTableView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.userInteractionEnabled = YES;
        self.backgroundColor = [UIColor whiteColor];
        self.scrollEnabled = NO;        //不可滑动，这很重要
        
        self.delegate = self;
        self.dataSource = self;
        [self registerClass:[RelativeNewsTableViewCell class] forCellReuseIdentifier:kRelativeNewsTableViewCellId];
    }
    
    return self;
}


- (void)setRelativeNewsArray:(NSArray *)relativeNewsArray {
    if (_relativeNewsArray != relativeNewsArray) {
        _relativeNewsArray = [relativeNewsArray copy];
        
        [self resetTableHeaderView];
        [self reloadData];
    }
}

//相关新闻 字样，和关键字  作为头部视图
- (void)resetTableHeaderView {
    UIView* headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidth, relativeCellHeight)];
    headerView.userInteractionEnabled = YES;
    //相关新闻 红背景
    UIImageView* relativeImgView = [[UIImageView alloc] initWithFrame:CGRectMake(-20, 0, 90, 25)];
    relativeImgView.backgroundColor = kNetEaseRedColor;
    relativeImgView.layer.cornerRadius = 12.5;
    [headerView addSubview:relativeImgView];
    //相关新闻字样
    UILabel* relativeLabel = [[UILabel alloc] initWithFrame:relativeImgView.frame];
    relativeLabel.text = @"相关新闻";
    relativeLabel.transform = CGAffineTransformMakeTranslation(5, 0);
    relativeLabel.textColor = [UIColor whiteColor];
    relativeLabel.textAlignment = NSTextAlignmentCenter;
    relativeLabel.font = [UIFont systemFontOfSize:12];
    [headerView addSubview:relativeLabel];
    
    
    //关键字
    NSArray* keyWordsArray = [_keyWords componentsSeparatedByString:@","];
    if (keyWordsArray.count) {
        CGFloat x = 25.0;
        for (int i = 0; i < keyWordsArray.count; i++) {
            NSString* str = keyWordsArray[i];
            if (str.length) {
                CGFloat width = str.length*14+20;
                UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(x, 35, width, 30)];
                [button setBackgroundColor:[UIColor colorWithRed:219.0/255 green:236.0/255 blue:243.0/255 alpha:1]];
                button.layer.cornerRadius = 15;
                [button setTitle:str forState:UIControlStateNormal];
                [button setTitleColor:[UIColor colorWithRed:49.0/255 green:154.0/255 blue:251.0/255 alpha:1] forState:UIControlStateNormal];
                button.titleLabel.font = [UIFont systemFontOfSize:14];
                [headerView addSubview:button];
                
                x += (width+15);
            }
        }   
    }
    
    self.tableHeaderView = headerView;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _relativeNewsArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    RelativeNewsTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:kRelativeNewsTableViewCellId forIndexPath:indexPath];
    
    cell.backgroundColor = [UIColor clearColor];
    cell.relativeNewsDic = _relativeNewsArray[indexPath.row];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return relativeCellHeight;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSDictionary* dic = _relativeNewsArray[indexPath.row];
    //根据响应者链找到NewsDetailVC这个类，push到要观看的新闻
    NewsDetailViewController* newsDetailVC = [Tools className:@"NewsDetailViewController" fromResponder:self.nextResponder];
    if (newsDetailVC) {
        NewsDetailViewController* relativeNewsVC = [[NewsDetailViewController alloc] init];
        relativeNewsVC.newsId = dic[@"id"];
        [newsDetailVC.navigationController pushViewController:relativeNewsVC animated:YES];
    }
}

@end
